package com.cos.shop.util;

/**
 * @author: COS
 * @time: 2023/5/11 10:22
 * @description:
 */
public class PageHelper<T> {
    private int pageNum;
    private int pageSize;
    private int totalPage;
    private int totalCount;
    private int rowStart;

    private T list;

    public PageHelper(int pageNum, int pageSize, int totalCount) {
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        // 计算总页数
        this.totalPage = (int) Math.ceil((this.totalCount + 0.0) / this.pageSize);
        //总页数 防止没有数据导致sql异常
        if (this.totalPage < 1) {
            this.totalPage = 1;
        }
        //校验页码
        if (pageNum < 1) {
            this.pageNum=1;
        } else if (pageNum > this.totalPage) {
            this.pageNum=this.totalPage;
        } else {
            this.pageNum = pageNum;
        }
        // 计算起始行
        this.rowStart=(this.pageNum-1)*this.pageSize;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getRowStart() {
        return rowStart;
    }

    public void setRowStart(int rowStart) {
        this.rowStart = rowStart;
    }

    public T getList() {
        return list;
    }

    public void setList(T list) {
        this.list = list;
    }
}
